package ecom.beans.dao;

import ecom.beans.ProductcategoryBean;
import ecom.beans.exception.DAONotUniqueException;
import ecom.beans.exception.ProductcategoryException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public class ProductcategoryDAO extends DAO {

    public static Long saveProductcategory(ProductcategoryBean productcategory) throws DAONotUniqueException, ProductcategoryException {
        Long result = null;
        if (getByName(productcategory.getName()) == null) {
            if (productcategory.getName().length() > 50) {
                throw new ProductcategoryException("The name of the productcategory should not exceed 50 characters");
            }
            saveOrUpdate(productcategory);
            result = productcategory.getIdProductcategory();
        } else {
            throw new DAONotUniqueException("The name of the product category is not unique");
        }
        return result;
    }

    public static void deleteProductcategory(ProductcategoryBean productcategory) {
        delete(productcategory);
    }

    public static void updateProductcategory(ProductcategoryBean productcategory) {
        saveOrUpdate(productcategory);
    }

    public static Set getAllProductcategories(String order) {
        return fetchAll("ProductcategoryBean.findAll", null, order);
    }

    public static ProductcategoryBean getByIdProductcategory(Long idProductcategory) {
        List parameters = new ArrayList();
        parameters.add("idProductcategory");
        parameters.add(idProductcategory);
        return (ProductcategoryBean) fetchOne("ProductcategoryBean.findByIdProductcategory", parameters);
    }

    public static ProductcategoryBean getByName(String name) {
        List parameters = new ArrayList();
        parameters.add("name");
        parameters.add(name);
        return (ProductcategoryBean) fetchOne("ProductcategoryBean.findByName", parameters);
    }
}
